package com.gaowl.gulimall.product.service.impl;

import com.gaowl.gulimall.product.entity.SpuInfoEntity;
import com.gaowl.gulimall.product.vo.Images;
import com.gaowl.gulimall.product.vo.Skus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaowl.common.utils.PageUtils;
import com.gaowl.common.utils.Query;

import com.gaowl.gulimall.product.dao.SkuInfoDao;
import com.gaowl.gulimall.product.entity.SkuInfoEntity;
import com.gaowl.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        // 关键字模糊查询
        // select * from pms_sku_info where brandId = ? and publish_status = ? and catelog_id = ?
        //  and (id=key or spu_name like %key%)
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            queryWrapper.and(w-> w.like("sku_name", key).or().eq("sku_id", key));
        }

        String brandId = (String) params.get("brandId");
        // 无条件时brandId默认为0，此时查询全部品牌
        if (!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)){
            queryWrapper.eq("brand_id", brandId);
        }

        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)){
            queryWrapper.eq("catalog_id", catelogId);
        }

        String minPrice = (String) params.get("min");
        if (!StringUtils.isEmpty(minPrice)){
            queryWrapper.ge("price", minPrice);
        }

        String maxPrice = (String) params.get("max");
        if (!StringUtils.isEmpty(maxPrice) && !"0".equalsIgnoreCase(minPrice)){
            queryWrapper.le("price", maxPrice);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params), queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 保存当前Spu对应的所有sku信息 之 保存sku基本信息
     * @param sku 待保存的sku基础信息
     * @param curSpuInfoEntity 当前Spu对象
     */
    @Override
    public Long saveBaseSkuInfo(Skus sku, SpuInfoEntity curSpuInfoEntity) {
        SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
        BeanUtils.copyProperties(sku, skuInfoEntity);
        skuInfoEntity.setSpuId(curSpuInfoEntity.getId());
        skuInfoEntity.setBrandId(curSpuInfoEntity.getBrandId());
        skuInfoEntity.setCatalogId(curSpuInfoEntity.getCatalogId());
        skuInfoEntity.setSaleCount(0L);

        String defaultImg = "";
        for(Images img : sku.getImages()){
            if (img.getDefaultImg() == 1) {
                defaultImg = img.getImgUrl();
            }
        }
        skuInfoEntity.setSkuDefaultImg(defaultImg);

        this.baseMapper.insert(skuInfoEntity);
        return skuInfoEntity.getSkuId();
    }

}